import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

/** 获取资产列表 */
export const getAssetList = ({
  data,
  type = "asset",
}: {
  data: object;
  type: string;
}) => {
  return taskRequest<Result>("post", `/api/api/${type}/data`, data);
};

/** 获取漏洞列表 */
export const getVulnerabilityList = (params: {
  page?: number;
  per_page?: number;
}) => {
  return taskRequest<Result>("get", `/api/avd_vulnerabilities?page=${params.page}&per_page=${params.per_page}`);
};

/** 获取漏洞详情 */
export const getVulnerabilityDetail = (avd_id: string) => {
  return taskRequest<Result>(
    "get",
    `/api/vulnerability_detail_search?avd_id=${avd_id}`
  );
};
